﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Xml;
using YJKMenuAssist.Utility;

namespace YJKMenuAssist
{
	public class CommandInfos
	{
		public string m_TabName { get; set; }

		public string TabName
		{
			get
			{
				return this.m_TabName;
			}
			set
			{
				this.m_TabName = value;
			}
		}

		public bool CmdShow
		{
			get
			{
				return this.m_bIsShow;
			}
			set
			{
				this.m_bIsShow = value;
			}
		}

		public List<CommandInfos> LstCommandInfos { get; set; }

		public string CmdId { get; set; }

		public string CmdName { get; set; }

		public string CmdText { get; set; }

		public string DllName { get; set; }

		public string ClassName { get; set; }

		public string ImageName { get; set; }

		public string Tooltip { get; set; }

		public string PanelName { get; set; }

		public string KeyBoardName { get; set; }

		public CommandInfos()
		{
			this.LstCommandInfos = new List<CommandInfos>();
		}

		public CommandInfos(string cmdName, string cmdText, string dllName, string className, string imageName, string tooltip, string panelName, bool cmdShow, string tabName)
		{
			this.CmdShow = cmdShow;
			this.CmdName = cmdName;
			this.CmdText = cmdText;
			this.DllName = dllName;
			this.ClassName = className;
			this.ImageName = imageName;
			this.Tooltip = tooltip;
			this.PanelName = panelName;
			this.TabName = tabName;
		}

		public CommandInfos(CommandInfos infos)
		{
			this.CmdShow = infos.CmdShow;
			this.CmdName = infos.CmdName;
			this.CmdText = infos.CmdText;
			this.DllName = infos.DllName;
			this.ClassName = infos.ClassName;
			this.ImageName = infos.ImageName;
			this.Tooltip = infos.Tooltip;
			this.PanelName = infos.PanelName;
		}

		public static System.Windows.Controls.Image GetImageByName(string imageFillName)
		{
			System.Windows.Controls.Image result;
			try
			{
				if (!File.Exists(imageFillName))
				{
					imageFillName = FileUtility.GetAppRootPath() + "\\Res\\HcHelp16.png";
				}
				System.Windows.Controls.Image image = new System.Windows.Controls.Image();
				Bitmap bmp = new Bitmap(imageFillName);
				image.Source = CommandInfos.ChangeBitmapToBitmapSource(bmp);
				image.HorizontalAlignment = HorizontalAlignment.Left;
				image.Margin = new Thickness(0.0, 5.0, 0.0, 0.0);
				result = image;
			}
			catch (Exception)
			{
				MessageBox.Show("竖向菜单资源'" + imageFillName + "'不存在");
				result = null;
			}
			return result;
		}

		private static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
		{
			BitmapSource result;
			try
			{
				result = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public bool Export2XmlNode(XmlElement xmlNode)
		{
			bool result;
			try
			{
				xmlNode.SetAttribute("Name", this.CmdName);
				xmlNode.SetAttribute("Text", this.CmdText);
				xmlNode.SetAttribute("DllName", this.DllName);
				xmlNode.SetAttribute("ClassName", this.ClassName);
				xmlNode.SetAttribute("MenuImage", this.ImageName);
				xmlNode.SetAttribute("ToolTip", this.Tooltip);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public bool SetAttribute(XmlElement xmlNode)
		{
			bool result;
			try
			{
				xmlNode.SetAttribute("Name", this.CmdName);
				string name = "IsShow";
				result = this.CmdShow;
				xmlNode.SetAttribute(name, result.ToString());
				xmlNode.SetAttribute("CommandId", this.CmdId);
				xmlNode.SetAttribute("ClassName", this.ClassName);
				xmlNode.SetAttribute("DllName", this.DllName);
				xmlNode.SetAttribute("TabName", this.TabName);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public virtual bool ImportByXmlNode(XmlNode xmlNode, string tabName)
		{
			try
			{
				if (this.CheckAttribute(xmlNode, "IsShow") == "True")
				{
					this.CmdShow = true;
				}
				else
				{
					this.CmdShow = false;
				}
				this.CmdName = this.CheckAttribute(xmlNode, "Name");
				this.CmdText = this.CheckAttribute(xmlNode, "Text");
				this.DllName = this.CheckAttribute(xmlNode, "DllName");
				this.ClassName = this.CheckAttribute(xmlNode, "ClassName");
				this.CmdId = this.CheckAttribute(xmlNode, "CommandId");
				this.ImageName = this.CheckAttribute(xmlNode, "ImageName");
				this.Tooltip = this.CheckAttribute(xmlNode, "ToolTip");
				this.TabName = tabName;
				if (xmlNode.Attributes["PanelName"] == null)
				{
					if (xmlNode.ParentNode.Attributes["Name"] == null)
					{
						this.PanelName = this.CheckAttribute(xmlNode.ParentNode.ParentNode, "Name");
					}
					else
					{
						this.PanelName = this.CheckAttribute(xmlNode.ParentNode, "Name");
					}
				}
				else
				{
					this.PanelName = this.CheckAttribute(xmlNode, "PanelName");
				}
			}
			catch
			{
				return false;
			}
			return true;
		}

		private string CheckAttribute(XmlNode xmlNode, string name)
		{
			string empty = string.Empty;
			if (xmlNode.Attributes[name] == null)
			{
				((XmlElement)xmlNode).SetAttribute(name, "");
			}
			return xmlNode.Attributes[name].Value;
		}

		private bool m_bIsShow = true;
	}
}
